#!/usr/bin/env python

class Trait(object):

    TYPE_PHYSICAL = 1
    TYPE_MENTAL = 2
    TYPE_SOCIAL = 4

    (
    KIND_MUNDANE,
    KIND_EXOTIC,
    KIND_SUPERNATURAL
    ) = range(3)

    def __init__(self, name=None):
        self.name = name
        self._ttype = Trait.TYPE_PHYSICAL
        self._kind = Trait.KIND_MUNDANE
        self.level = None
        self.cost = 0
        self.description = None

    def get_ttype(self):
        return self._type
    def set_ttype(self, ttype):
        self._ttype = 0
        if 'physical' in ttype:
            self._ttype = self._ttype or Trait.TYPE_PHYSICAL

        if 'mental' in ttype:
            self._ttype = self._ttype or Trait.TYPE_PHYSICAL

        if 'social' in ttype:
            self._ttype = self._ttype or Trait.TYPE_MENTAL
    ttype = property(get_ttype, set_ttype)

    def get_kind(self):
        return self._kind
    def set_kind(self, kind):
        if 'exotic' in kind:
            self._kind = Trait.KIND_EXOTIC
        elif 'supernatural' in kind:
            self._kind = Trait.KIND_SUPERNATURAL
        elif 'mundane' in kind:
            self._kind = Trait.KIND_MUNDANE
        else:
            raise Exception
    kind = property(get_kind, set_kind)

    def __str__(self):
        text = self.name
        if self.level:
            text += ' %d' % self.level
        if self.description:
            text += ' (%s)' % self.description
        text += ' [%d]' % self.cost
        return text


class Skill(object):

    (
    TYPE_NORMAL,
    TYPE_SPELL
    ) = range(2)

    def __init__(self, name=None, level=0):
        self.name = name
        self.level = level
        self.type = Skill.TYPE_NORMAL
        self.description = None

    def __str__(self):
        text = self.name
        if self.description:
            text += '(%s)' % self.description
        text += ' %d' % self.level
        if self.relativelevel:
            text += ' %s' % self.relativelevel
        text += ' [%d]' % self.cost
        return text


class Character(object):
    def __init__(self, name=None):
        self.name = name
        self.player = None
        self.created = None
        self.points_total = 0
        self.points_unspent = 0

        self.age = 0
        self.height = 0
        self.weight = 0
        self.sizemodifier = 0
        self.appearance = 'Average'

        self.st = 10
        self.dx = 10
        self.iq = 10
        self.ht = 10

        self.hp = 10
        self.will = 10
        self.per = 10
        self.fp = 10

        self.related_attribute = {
            'hp'   : 'st',
            'will' : 'iq',
            'per'  : 'iq',
            'fp'   : 'ht'
        }

        self.basicspeed = 0
        self.basicmove = 0

        self.damage_throw = None
        self.damage_swing = None

        self.advantages = None
        self.disadvantages = None
        self.skills = None
        self.spells = None

    def primary_attribute_cost(self, attribute):
        attribute = attribute.lower()
        if attribute in ('st', 'ht'):
            factor = 10
        elif attribute in ('dx' 'iq'):
            factor = 20
        return (self.__getattribute__(attribute) - 10) * factor

    def secondary_attribute_cost(self, attribute):
        attribute = attribute.lower()
        rattr = self.related_attribute[attribute]
        print attribute
        if attribute == 'hp':
            factor = 2
        if attribute in ('will', 'per'):
            factor = 5
        elif attribute == 'fp':
            factor = 3
        return (self.__getattribute__(attribute) - \
                self.__getattribute__(rattr)) * factor

    def __str__(self):
        advs = [str(adv) for adv in self.advantages]
        disadvs = [str(disadv) for disadv in self.disadvantages]
        skills = [str(skill) for skill in self.skills]
        #spells = [str(spell) for spell in self.spells]
        return '''-- Character --
Name: %s
Player: %s
Created: %s

Age: %d
Height: %s
Weight: %s
Size Modifier: %d
Appearance: %s

ST   %d (%d)
DX   %d (%d)
IQ   %d (%d)
HT   %d (%d)

HP   %d (%d)
WILL %d (%d)
PER  %d (%d)
FP   %d (%d)

Basic Speed: %.2f
Basic Move: %d
Basic Lift: %d

Basic Damage
Thr: %s
Sw : %s

Advantages: %s
Disadvantages: %s
Skills: %s
Spells: %s
''' % (self.name, self.player, self.created,
        self.age, self.height, self.weight, self.sizemodifier,
        self.appearance,
        self.st, self.primary_attribute_cost('st'),
        self.dx, self.primary_attribute_cost('dx'),
        self.iq, self.primary_attribute_cost('iq'),
        self.ht, self.primary_attribute_cost('ht'),
        self.hp, self.secondary_attribute_cost('hp'),
        self.will, self.secondary_attribute_cost('will'),
        self.per, self.secondary_attribute_cost('per'),
        self.fp, self.secondary_attribute_cost('fp'),
        self.basicspeed, self.basicmove, self.basiclift,
        self.damage_throw, self.damage_swing,
        advs, disadvs, skills, str([])
        )

